package com.example.myapplication;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.btn);
        button.setOnClickListener(this);
        initNotification();

        //添加返回栈  TaskStackBuilder

    }

    private void initNotification() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "chat";
            String channelName = "聊天消息";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance);

            channelId = "subscribe";
            channelName = "订阅消息";
            importance = NotificationManager.IMPORTANCE_DEFAULT;
            createNotificationChannel(channelId, channelName, importance);

        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                //当系统版本大于8.0
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    assert manager != null;
                    NotificationChannel channel = manager.getNotificationChannel("chat");
                    //判断当前渠道是否关闭
                    //如果关闭
                    if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
                        Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                        intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
                        intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
                        startActivity(intent);
                        Toast.makeText(this, "请手动将通知打开", Toast.LENGTH_SHORT).show();
                    } else {
                        Notification notification = new NotificationCompat.Builder(this, "chat")
                                .setContentTitle("收到一条聊天消息")
                                .setContentText("今天中午吃什么？")
                                .setWhen(System.currentTimeMillis())
                                .setSmallIcon(R.mipmap.ic_launcher)
                                // .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
                                .setAutoCancel(true)
                                .build();
                        manager.notify(1, notification);
                    }


                } else {
                    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                            MainActivity.this).setSmallIcon(R.drawable.ic_launcher_background)
                            .setContentTitle("5 new message")
                            .setContentText("twain@android.com");
                    mBuilder.setTicker("New message");//第一次提示消息的时候显示在通知栏上
                    mBuilder.setAutoCancel(true);//自己维护通知的消失
                    //获取通知管理器对象
                    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    assert mNotificationManager != null;
                    mNotificationManager.notify(0, mBuilder.build());
                }
                break;
        }
    }
}
